package com.yonyou.pmclouds.billtype.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.billtype.entity.BillTypeVO;
import com.yonyou.pmclouds.billtype.mapper.BillTypeVOMapper;
import com.yonyou.pmclouds.billtype.rmiitf.BillTypeQuery;
import com.yonyou.pmclouds.businessobject.entity.BusinessObjectVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * 单据类型查询
 *
 * @author yzx
 */
@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = BillTypeQuery.class, timeout = 7000)
public class BillTypeQueryImpl implements BillTypeQuery {
    @Autowired
    private BillTypeVOMapper mapper;

    @Override
    public List<BillTypeVO> getBillTypeVoListByPkTenant(String pkTenant) {
        return mapper.selectBillTypeByPkTenant(pkTenant);
    }

    /**
     * 构造单据类型对应表的树形关系
     *
     * @param pkTenant
     * @return
     */
    @Override
    public List<BillTypeVO> getBillTableNameRelation(String pkTenant) throws BusinessException {
        // 数据结构列表
        List<BillTypeVO> treeList = new ArrayList<>();
        // 获取主表的所有数据
        List<BillTypeVO> headTableList = mapper.selectBillTypeByPkTenant(pkTenant);
        // 获取主表对应子表所有数据
        List<BillTypeVO> bodyTableList = mapper.selectNoBillTypeAllData();
        // 构造出要求的数据结构
        for (BillTypeVO billTypeVO : headTableList) {
            billTypeVO.setSubList(getSubList(billTypeVO.getPkBilltype(), bodyTableList));
            treeList.add(billTypeVO);
        }
        return treeList;
    }

    private ArrayList<BillTypeVO> getSubList(String pkBillType, List<BillTypeVO> voList) {
        ArrayList<BillTypeVO> childList = new ArrayList<>();
        String parentId;
        // 子表的直接子对象
        for (BillTypeVO billTypeVO : voList) {
            parentId = billTypeVO.getPkParent();
            if (pkBillType.equals(parentId)) {
                childList.add(billTypeVO);
            }
        }
        // 子表的间接子对象
        for (BillTypeVO billTypeVO : childList) {
            billTypeVO.setSubList(getSubList(billTypeVO.getPkBilltype(), voList));
        }
        // 递归退出条件
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }

    @Override
    public List<BusinessObjectVO> constructBusinessObjsRelation(String pkTenant) throws BusinessException {
        // 结果集
        List<BusinessObjectVO> result = new ArrayList<>();
        // 数据结构列表
        List<BillTypeVO> treeList = getBillTableNameRelation(pkTenant);
        for (BillTypeVO treeNode : treeList) {
            BusinessObjectVO objectVO = new BusinessObjectVO();
            objectVO.setBo_name(treeNode.getSname());
            objectVO.setBo_code(treeNode.getTableName());
            if(StringUtils.isNotEmpty(pkTenant)){
                objectVO.setTenant_id(pkTenant);
            }
            objectVO.setSub_bos(constructSubList(treeNode.getSubList(), pkTenant));
            result.add(objectVO);
        }
        return result;
    }

    private ArrayList<BusinessObjectVO> constructSubList(ArrayList<BillTypeVO> subList, String pkTenant) {
        if (subList == null) {
            return null;
        }
        // 构造子表业务对象数据
        ArrayList<BusinessObjectVO> objectVOSList = new ArrayList<>();
        for (BillTypeVO treeNode : subList) {
            BusinessObjectVO objectVO = new BusinessObjectVO();
            objectVO.setBo_name(treeNode.getSname());
            objectVO.setBo_code(treeNode.getTableName());
            if(StringUtils.isNotEmpty(pkTenant)){
                objectVO.setTenant_id(pkTenant);
            }
            objectVO.setSub_bos(constructSubList(treeNode.getSubList(), pkTenant));
            objectVOSList.add(objectVO);
        }
        return objectVOSList;
    }

    /**
     * 根据单据类型查询单据信息
     * @param scode 单据类型
     * @param dataUsage datumManage为整理资料时使用，approvalCloud为云审批使用
     * @return
     * @throws BusinessException
     */
    @Override
    public BillTypeVO queryBillByType(String scode, String dataUsage) throws BusinessException {
        return mapper.selectBillByType(scode, dataUsage);
    }
    /**
     * 和mapper.selectBillTypeByPkTenant区分开，那个为资料管理使用，这个为云审批和云打印使用，
     * 过滤条件不一致
     */
    @Override
    public BillTypeVO[] queryBillTypeByTenantId(String pkTenant) throws BusinessException {
        return mapper.selectBillTypeByTenantId(pkTenant);
    }

    @Override
    public List<BillTypeVO> queryByCodes(String[] codes) throws BusinessException {
        return mapper.queryByCodes(codes);
    }
}
